setClass("MDSReducer",contains="Reducer",
		representation(
				.dataDimsMatrix="matrix", #these are distances between points
				.distanceType = "character",
				.minkowskiParam ="numeric"
		),
		prototype=prototype(
				.description="MDS Reducer",
				.dataDimsMatrix=matrix(),
				.distanceType = "minkowski",
				.minkowskiParam =5

		),
		validity=function(.Object){
			if(! is(.Object@.dimChooser,"MDSDimChooser")){
				stop("MDSReducer must be initialized by MDSDimChooser object. MDSReducer was initialized by ",class(.Object@.dimChooser))
			}
		}
)

#methods

setMethod("initialize",
		signature="MDSReducer",
		function(.Object,dimChooser,distanceType="minkowski",minkowskiParam=5,...){
			.Object <-callNextMethod(.Object,dimChooser,...);
			.Object@.distanceType <-distanceType
			.Object@.minkowskiParam <- minkowskiParam
			.Object@.description <- paste(.Object@.description,.Object@.distanceType,sep=" ")
			return(.Object)
		})


setMethod("reduce",
		signature="MDSReducer",
		definition=function(.Object,inputSet,...){
			.Object@.tmpData		<-as.matrix(inputSet)
			dims <- dim(.Object@.tmpData)
			rows <- dims[1]
			cols <- dims[2]
			.Object@.dataDimsMatrix <- as.matrix(dist(.Object@.tmpData,method=.Object@.distanceType,p=.Object@.minkowskiParam))
			
			dim <-chooseBest(.Object@.dimChooser,.Object)
			retSet <- cmdscale(.Object@.dataDimsMatrix,k=dim)
			
			
			.Object@.tmpData <- matrix()
			.Object@.dataDimsMatrix <-matrix()
			.Object@.resEnv$savedSet <-as.matrix(retSet)
			return(.Object@.resEnv$savedSet)
			
		})  
